//
//  FloatView.m
//  project
//
//  Created by 张淋 on 2019/5/7.
//  Copyright © 2019 黄建华. All rights reserved.
//

#import "FloatView.h"

@interface FloatView ()

@property (nonatomic,assign) BOOL isMoving;  // 标识移动中

@end

@implementation FloatView

#pragma mark - lazyload
- (UIImageView *)timeImageView {
    if (_timeImageView == nil) {
        _timeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height/ 2)];
        UIImage *image = [UIImage imageNamed:@"icon_wallpaper_widget_time"];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        _timeImageView.image = image;
    }
    return _timeImageView;
}

- (UIImageView *)stepImageView {
    if (_stepImageView == nil) {
        _stepImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height/ 2, self.bounds.size.width, self.bounds.size.height/ 2)];
        UIImage *image = [UIImage imageNamed:@"icon_wallpaper_widget_step"];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        _stepImageView.image = image;
    }
    return _stepImageView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.timeImageView];
        [self addSubview:self.stepImageView];
    }
    _isMoving = NO;
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    //CGPoint pt = [touch locationInView:self.superview];
    CGRect frame = self.frame;
    //在自身视图中的位置
    CGPoint pt = [touch locationInView:self];
    CGPoint anchorPoint = CGPointMake(pt.x/self.bounds.size.width, pt.y/self.bounds.size.height);
    //这是设置的是position也就是center的位置百分比
    //锚点
    self.layer.anchorPoint = anchorPoint;
    self.frame = frame;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    CGPoint pt = [touch locationInView:self.superview];
    CGFloat padding =  -20;
    CGRect image = self.superview.bounds;
    CGRect frame = self.frame;
    if (frame.origin.x >= padding && frame.origin.x <= (image.size.width-frame.size.width-padding) && frame.origin.y >= padding && frame.origin.y <= (image.size.height-frame.size.height-padding)) {
        self.layer.position = pt;
    }
    
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self limitRangeOfView];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self limitRangeOfView];
}

- (void)limitRangeOfView {
    [UIView animateWithDuration:0.3 animations:^{
        
        CGFloat padding = 0;
        CGRect image = self.superview.bounds;
        CGRect frame = self.frame;
        if (frame.origin.x <= padding) {
            frame.origin.x = padding;
        }
        if (frame.origin.y <= padding) {
            frame.origin.y = padding;
        }
        if (frame.origin.x >= (image.size.width-frame.size.width-padding)) {
            frame.origin.x = image.size.width-frame.size.width-padding;
        }
        if (frame.origin.y >= (image.size.height-frame.size.height-padding)) {
            frame.origin.y = image.size.height-frame.size.height-padding;
        }
        
        self.frame = frame;
    }];
}

@end
